package hiresoft.ui;

import hiresoft.network.*;
import hiresoft.users.User;
import java.util.prefs.BackingStoreException;
import javax.swing.*;

/**
 * Creates the User Interface and
 *  functionality for all accounts
 *
 * @author Kevin Carlino
 */
public class LoginFrame extends JFrame {
    int isApplicant;

    public LoginFrame(User _user) {
        super();
        initComponents();
        lblHireSoft.requestFocus(true);
        this.setLocationRelativeTo(null);
    }

    public void clearFields(){
        txtUsername.setText("Username");
        txtPassword.setText("Password");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnLogin = new javax.swing.JButton();
        chkIsApplicant = new javax.swing.JCheckBox();
        lblDevelopers = new javax.swing.JLabel();
        lblHireSoft = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();
        txtUsername = new javax.swing.JTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        exitMenuItem = new javax.swing.JMenu();
        networkMenuItem = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        btnLogin.setText("Login");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        chkIsApplicant.setText("Applicant?");
        chkIsApplicant.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkIsApplicantActionPerformed(evt);
            }
        });

        lblDevelopers.setText("Ex Dragon Developers");

        lblHireSoft.setFont(new java.awt.Font("Trebuchet MS", 1, 24));
        lblHireSoft.setText("HireSoft");

        txtPassword.setText("Password");
        txtPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPasswordActionPerformed(evt);
            }
        });
        txtPassword.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtPasswordFocusGained(evt);
            }
        });
        txtPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPasswordKeyPressed(evt);
            }
        });

        txtUsername.setText("Username");
        txtUsername.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtUsernameFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtUsernameFocusLost(evt);
            }
        });

        exitMenuItem.setText("File");

        networkMenuItem.setText("Config");
        networkMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                networkMenuItemActionPerformed(evt);
            }
        });
        exitMenuItem.add(networkMenuItem);

        jMenuItem1.setText("Exit");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        exitMenuItem.add(jMenuItem1);

        jMenuBar1.add(exitMenuItem);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblDevelopers, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(102, 102, 102)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnLogin, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(chkIsApplicant)
                            .addComponent(txtPassword)
                            .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(136, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(145, 145, 145)
                .addComponent(lblHireSoft, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(163, 163, 163))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(lblHireSoft, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addComponent(chkIsApplicant)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnLogin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83, Short.MAX_VALUE)
                .addComponent(lblDevelopers))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        login();
    }//GEN-LAST:event_btnLoginActionPerformed

    private void chkIsApplicantActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkIsApplicantActionPerformed
        if(isApplicant == 1) {
            isApplicant = 0;
            txtUsername.setEnabled(true);
            txtPassword.setEnabled(true);
        }
        else {
            isApplicant = 1;
            txtUsername.setEnabled(false);
            txtPassword.setEnabled(false);
        }
    }//GEN-LAST:event_chkIsApplicantActionPerformed

    private void txtPasswordFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPasswordFocusGained
        if(txtPassword.getText().equals("Password"))
            txtPassword.setText("");

    }//GEN-LAST:event_txtPasswordFocusGained

    private void txtUsernameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtUsernameFocusGained
        if(txtUsername.getText().equals("Username"))
            txtUsername.setText("");
    }//GEN-LAST:event_txtUsernameFocusGained

    private void networkMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_networkMenuItemActionPerformed
        String netconf = JOptionPane.showInputDialog("Please enter new network configuration: (current:" + Network.getIP() + ":" + Network.getPort() + ")");
        if(netconf != null){
            String[] net = netconf.split(":");
            try{
                Network.setDefaultIP(net[0]);
                Network.setDefaultPort(Integer.parseInt(net[1]));
            }catch (BackingStoreException e){
                JOptionPane.showMessageDialog(null, "Error: " + e);
        }
        }
    }//GEN-LAST:event_networkMenuItemActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void txtPasswordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPasswordKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPasswordKeyPressed

    private void txtPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPasswordActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPasswordActionPerformed

    private void txtUsernameFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtUsernameFocusLost
        if(txtUsername.getText().isEmpty())
            txtUsername.setText("Username");
    }//GEN-LAST:event_txtUsernameFocusLost

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoginFrame(null).setVisible(true);
            }
        });
    }

    /**
     * Sets the network/connection
     * Handles creating the proper UI based on account access
     */
    public void login() {
        try {
        Network net = Network.getInstance();
        net.out.Insert(isApplicant);

        if(isApplicant == 1) {
            net.out.Insert("");
            net.out.Insert("");
        }
        else {
            net.out.Insert(txtUsername.getText());
            net.out.Insert(txtPassword.getText());
        }

        net.out.Send(Packets.P_LOGIN);
        if (net.Ready())
        {
            if (net.in.getPacketId() == Packets.P_LOGIN)
            {
                    int Access = net.in.getDWord();
                    switch(Access) {
                        case 1:
                                ApplicantFrame applicant = new ApplicantFrame(this, null);
                                applicant.setVisible(true);
                                applicant.setLocationRelativeTo(null);
                                this.setVisible(false);
                                applicant.connect();
                                break;
                       case 2:
                                RecruiterFrame recruiter = new RecruiterFrame(this, null);
                                recruiter.setVisible(true);
                                recruiter.setLocationRelativeTo(null);
                                this.setVisible(false);
                                recruiter.connect();
                                break;
                       case 3:
                                ReviewerFrame reviewer = new ReviewerFrame(this, null);
                                reviewer.setVisible(true);
                                reviewer.setLocationRelativeTo(null);
                                this.setVisible(false);
                                reviewer.connect();
                                break;
                       case 4:
                                ReferenceFrame reference = new ReferenceFrame(this, null);
                                reference.setVisible(true);
                                reference.setLocationRelativeTo(null);
                                this.setVisible(false);
                                reference.connect();
                                break;
                       case 5:
                                AdminFrame admin = new AdminFrame(this, null);
                                admin.setVisible(true);
                                admin.setLocationRelativeTo(null);
                                this.setVisible(false);
                                admin.connect();
                                break;
                      default:
                                JOptionPane.showMessageDialog(null, "Your account information is incorrect, please try again.");
                    }
            }
        }
    }
    catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Error: " + e);
    }
}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLogin;
    private javax.swing.JCheckBox chkIsApplicant;
    private javax.swing.JMenu exitMenuItem;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JLabel lblDevelopers;
    private javax.swing.JLabel lblHireSoft;
    private javax.swing.JMenuItem networkMenuItem;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables

}
